var dataTableConfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/warn_rule/queryWarnRule",
		columns : [
				{
					data : "rule_id",
					render : function(data, type, full) {
						return '<input name=\"items\" type="checkbox" id=\"check_'+data+'\" value="' + data + '"/>';
					}
				}, {
					"data" : "rule_id"
				},{
					"data" : "name"
				},{
					"data" : "warn_type_name"
				},{
					"data" : "obj_type_name"
				},{
					"data" : "obj_name"
				},{
					"data": "kpi_name",
				},{
					"data": "period",
				},{
					"data": "template",
				},{ 
					"data": "access_type_code",
				},{
					"data": "obj_id",
				},{
					"data": "type_id",
				},{
					"data": "device_type_name",
				},{
					"data": "kpi_id",
				},{
					"data": "threshold",
				},{
					"data": "expr",
				},{
					"data": "obj_type",
				},{
					"data": "componet",
				},{
					"data": "componet_name",
				},{
					"data": "warn_type_id",
				},{
					"data": "access_id",
				}],
		columnDefs: [    
		        {    
		        	"targets": [10,11,12,13,14,15,16,17,18,19,20],    
		        	"visible": false    
		        }], 
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",
};

//初始化表格
var table = $('#warn_rule_table').DataTable(dataTableConfig);

//全选
$(function() {
	//全选
	$("#checkall_warn_rule").click(function() {
		$('[name=items]:checkbox').prop("checked", this.checked);
	});
	$('[name=items]:checkbox').click(
		function() {
			var $tmp = $('[name=items]:checkbox');
			$('#checkall_warn_rule').prop('checked',
					$tmp.length == $tmp.filter(':checked').length);
		}	
	);
});

//树设置
var setting = {
		async: {
			enable: true,
			url:"/kpiDefine/getTreeData",
			autoParam:["id", "name"],
			dataFilter: filter
		},
		callback: {
			onClick:nodeOnClick
		}
};

var filter = function filter(treeId, parentNode, childNodes) {
	if (!childNodes) return null;
	for (var i=0, l=childNodes.length; i<l; i++) {
		childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
	}
	return childNodes;
};


function nodeOnClick(event, treeId, treeNode, clickFlag) {
	var url = '/warn_rule/queryWarnRule';
	var data = {};
	var tid =treeNode.id;
	if(tid.indexOf('type') != -1){
		data.tree_type_id = tid.substring(0, tid.indexOf('_type'));
		$('#warn_rule_table').dataTable().api().destroy();
		dataTableConfig.ajax={"url": url, "data":data};
		dataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_warn_rule').removeAttr("disabled");
		};
		table=$('#warn_rule_table').dataTable(dataTableConfig);
		document.getElementById("type_id_warn_rule").options[0].selected=true;
		document.getElementById("type_id_warn_rule").options[0].value = tid.substring(0, tid.indexOf('_type'));
		document.getElementById("type_id_warn_rule").options[0].text= treeNode.name;
		document.getElementById("obj_type_warn_rule").options[1].selected=true;
	}
	
};			


var app = angular.module('warnRuleApp', []);
var keepDataFlag = false;		//判断关闭模态框时，是否刷新模态框的内容
var fromToUpdateModel = false;  //判断是否来自更新模态框

var initController = app.controller("warnRuleController" , function($scope, $http){
	$("#obj_type_update_warn_rule").change(function(){
		document.getElementById("type_id_warn_rule").options[0].selected=true;
		document.getElementById("type_id_warn_rule").options[0].value = "";
		document.getElementById("type_id_warn_rule").options[0].text= "";
		if(this.value=="0"){
			$('#type_id_update_warn_rule').removeAttr("disabled");
			$("#obj_id_update_warn_rule").empty();
			$http({
				url : '/deviceManager/getDeviceTypeList',
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				$scope.deviceTypeList = data;
				for (var i=0, l=data.length; i<l; i++) {
					var option = $("<option>").val(data[i].type_id).text(data[i].name);
					$('#type_id_update_warn_rule').append(option);
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
			});
			$scope.objList = {};
		}else if(this.value=="1"){
			$('#type_id_update_warn_rule').empty();
			
			$('#type_id_update_warn_rule').attr("disabled", "true");
			var objType = this.value;
			$http({
				url : '/warn_rule/getObjList',
				params: {objType:objType},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				$scope.objList = data;
				$("#obj_id_update_warn_rule").empty();
				for (var i=0, l=data.length; i<l; i++) {
					var option = $("<option>").val(data[i].id).text(data[i].name);
					$('#obj_id_update_warn_rule').append(option);
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
			});
		}
	});
	$("#type_id_update_warn_rule").change(function(){
		var objType = "0";
		var typeIdSelect =document.getElementById("type_id_update_warn_rule");
		var typeId = typeIdSelect.value;
		$http({
			url : '/warn_rule/getObjList',
			params:{objType:objType, typeId:typeId},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			$scope.objList = data;
			$("#obj_id_update_warn_rule").empty();
			for (var i=0, l=data.length; i<l; i++) {
				var option = $("<option>").val(data[i].id).text(data[i].name);
				$('#obj_id_update_warn_rule').append(option);
			}
		}).error(function(data, header, config, status) {
			// 处理响应失败
		});
	});
	$('#addModal_warn_rule').on('hide.bs.modal', function () {
		if(!keepDataFlag){
			document.getElementById("type_id_warn_rule").options[0].selected=true;
			document.getElementById("type_id_warn_rule").options[0].value = "";
			document.getElementById("type_id_warn_rule").options[0].text= "";
			document.getElementById("addForm_warn_rule").reset();
		}
	});
	$('#kpi_select_model').on('hide.bs.modal', function () {
		if(fromToUpdateModel){
			fromToUpdateModel = false;
			$('#updateModal_warn_rule').modal("toggle");
		}else{
			keepDataFlag = false;
			$('#addModal_warn_rule').modal("toggle");
		}
		
	});
	$.fn.zTree.init($("#treeDemo"), setting);
	$http({
		url : '/warn_rule/getWarnTypeList',
		method : 'GET'
	}).success(function(data, header, config, status) {
		// 响应成功
		$scope.warnTypeList = data;
	}).error(function(data, header, config, status) {
		// 处理响应失败
	});
	$http({
		url : '/deviceManager/getDeviceTypeList',
		method : 'GET'
	}).success(function(data, header, config, status) {
		// 响应成功
		$scope.deviceTypeList = data;
	}).error(function(data, header, config, status) {
		// 处理响应失败
	});
	$http({
		url : '/kpiTask/getKpiDefineList',
		method : 'GET'
	}).success(function(data, header, config, status) {
		// 响应成功
		$scope.kpiList = data;
	}).error(function(data, header, config, status) {
		// 处理响应失败
	});
	$http({
		url : '/kpiTask/getComponetList',
		method : 'GET'
	}).success(function(data, header, config, status) {
		// 响应成功
		$scope.componetList = data;
	}).error(function(data, header, config, status) {
		// 处理响应失败
	});
	$scope.add = function(){
		var form = document.getElementById("addForm_warn_rule");
		var name = form.name_warn_rule.value;
		var warn_type_id = form.warn_type_id_warn_rule.value;
		var obj_type = form.obj_type_warn_rule.value;
		var type_id = form.type_id_warn_rule.value;
		var obj_id = form.obj_id_warn_rule.value;
		var kpi_id = form.kpi_id_warn_rule.value;
		var threshold = form.threshold_warn_rule.value;
		var expr = form.expr_warn_rule.value;
		var period = form.period_warn_rule.value;
		var template = form.template_warn_rule.value;
		var componet = form.componet_warn_rule.value;
		var access = form.access_warn_rule.value;
		$http({
			url : '/warn_rule/add',
			params:{name:name, warn_type_id:warn_type_id, obj_type:obj_type , type_id:type_id, obj_id:obj_id,
				kpi_id:kpi_id, threshold:threshold, expr:expr, period:period, template:template, componet:componet, access:access},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			if(data=="1"){
				$('#addSuccessAlert_warn_rule').fadeIn(2000);
				$('#addSuccessAlert_warn_rule').fadeOut(2000);
				table.draw();
			}else{
				$('#addFailedAlert_warn_rule').fadeIn(2000);
				$('#addFailedAlert_warn_rule').fadeOut(2000);
			}
		}).error(function(data, header, config, status) {
			// 处理响应失败
			$('#addFailedAlert_warn_rule').fadeIn(2000);
			$('#addFailedAlert_warn_rule').fadeOut(2000);
		});
	};
	$scope.del_init=function(){
		var ruleIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			ruleIds += $(this).val() + ";";
		});
		if(ruleIds==""){
			$('#selAlert_warn_rule').html("<strong>请选择记录进行删除！</strong>");
			$('#alertModal_warn_rule').modal("toggle");
		}else{
			$("#delModal_warn_rule").modal("toggle");
		}
		
	};
	$scope.del = function(){
		var ruleIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			ruleIds += $(this).val() + ";";
		});
		if(ruleIds==""){
			alert("请选择要删除的记录！");
		}else{
			$http({
				url : '/warn_rule/delete',
				params:{ruleIds:ruleIds},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				if(data==true){
					$('#promptContent_warn_rule').hide();
					$('#del_warn_rule').hide();
					$('#deleteSuccessAlert_warn_rule').fadeIn(2000);
					$('#delModal_warn_rule').on('hide.bs.modal', function () {
						table.draw();
						$('#del_warn_rule').show();
						$('#deleteSuccessAlert_warn_rule').hide();
						$('#promptContent_warn_rule').show();
					});
				}else{
					$('#promptContent_warn_rule').hide();
					$('#del_warn_rule').hide();
					$('#deleteFailedAlert_warn_rule').fadeIn(2000);
					$('#delModal_warn_rule').on('hide.bs.modal', function () {
						table.draw();
						$('#del_warn_rule').show();
						$('#deleteFailedAlert_warn_rule').hide();
						$('#promptContent_warn_rule').show();
					});
				}
				
			}).error(function(data, header, config, status) {
				// 处理响应失败
				$('#promptContent_warn_rule').hide();
				$('#del_warn_rule').hide();
				$('#deleteFailedAlert_warn_rule').fadeIn(2000);
				$('#delModal_warn_rule').on('hide.bs.modal', function () {
					$('#del_warn_rule').show();
					$('#deleteFailedAlert_warn_rule').hide();
					$('#promptContent_warn_rule').show();
				});
			});
		}
	};
	$scope.update_init =function(){
		document.getElementById("updateForm_warn_rule").reset();
		var ruleIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			ruleIds += $(this).val() + ";";
		});
		if(ruleIds==""){
			$('#selAlert_warn_rule').html("<strong>请选择要修改的记录！</strong>");
			$('#alertModal_warn_rule').modal("toggle");
		}else if(ruleIds.split(";").length>2){
			$('#selAlert_warn_rule').html("<strong>修改的记录数不能大于1.</strong>");
			$('#alertModal_warn_rule').modal("toggle");
		}else{
			 $("[name=items]:checkbox").each(function(key,value){
		            if($(value).prop('checked')){
		            	var selRow =$('#warn_rule_table').dataTable().fnGetData(key-1);
		            	$scope.rule_id_update = selRow.rule_id;
		            	$scope.name_update = selRow.name;
		            	$scope.warn_type_name_update = selRow.warn_type_name;
		            	$scope.obj_type_name_update = selRow.obj_type_name;
		            	$scope.obj_name_update = selRow.obj_name;
		            	$scope.kpi_name_update = selRow.kpi_name;
		            	$scope.kpi_id_update = selRow.kpi_id;
		            	$scope.period_update = selRow.period;
		            	$scope.template_update = selRow.template;
		            	$scope.obj_id_update = selRow.obj_id;
		            	$scope.type_id_update = selRow.type_id;
		            	$scope.threshold_update = selRow.threshold;
		            	$scope.expr_update = selRow.expr;
		            	$scope.period_update = selRow.period;
		            	$scope.obj_type_update = selRow.obj_type;
		            	$scope.type_name_update = selRow.device_type_name;
		            	$scope.componet_update = selRow.componet;
		            	$scope.componet_name_update = selRow.componet_name;
		            	$scope.warn_type_id_update = selRow.warn_type_id;
		            	$scope.access_update = selRow.access_id;
		            }
			 });
			 $("#updateModal_warn_rule").modal("toggle");
		}
	};
	$scope.update = function(){
		var form = document.getElementById("updateForm_warn_rule");
		var rule_id = form.rule_id_update_warn_rule.value;
		var name = form.name_update_warn_rule.value;
		var warn_type_id = form.warn_type_id_update_warn_rule.value;
		var obj_type = form.obj_type_update_warn_rule.value;
		var type_id = form.type_id_update_warn_rule.value;
		var obj_id = form.obj_id_update_warn_rule.value;
		var kpi_id = form.kpi_id_update_warn_rule.value;
		var threshold = form.threshold_update_warn_rule.value;
		var expr = form.expr_update_warn_rule.value;
		var period = form.period_update_warn_rule.value;
		var template = form.template_update_warn_rule.value;
		var componet = form.componet_update_warn_rule.value;
		var access = form.access_update_warn_rule.value;
		$http({
			url : '/warn_rule/edit',
			params:{rule_id:rule_id, name:name, warn_type_id:warn_type_id, obj_type:obj_type , 
				type_id:type_id, obj_id:obj_id, kpi_id:kpi_id, threshold:threshold, expr:expr,
				period:period, template:template, componet:componet, access:access},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			if(data=="1"){
				$('#updateSuccessAlert_warn_rule').fadeIn(2000);
				$('#updateSuccessAlert_warn_rule').fadeOut(2000);
				table.draw();
			}else{
				$('#updateFailedAlert_warn_rule').fadeIn(2000);
				$('#updateFailedAlert_warn_rule').fadeOut(2000);
			}
		}).error(function(data, header, config, status){
			// 处理响应失败
			$('#updateFailedAlert_warn_rule').fadeIn(2000);
			$('#updateFailedAlert_warn_rule').fadeOut(2000);
		});
	};
	
	$scope.query = function () {
		var url = '/warn_rule/queryWarnRule';
		var data = {};
		var form =document.getElementById("queryForm_warn_rule");  // $('#queryForm');
		if(form.name_query_warn_rule.value!=0){
			data.name =  form.name_query_warn_rule.value;
		}
		if(form.warn_type_query_warn_rule.value!=0){
			data.warn_type = form.warn_type_query_warn_rule.value;
		}
		if(form.device_type_query_warn_rule.value!=0){
			data.device_type=  form.device_type_query_warn_rule.value;
		}
		if(form.obj_type_query_warn_rule.value!=0){
			data.obj_type =  form.obj_type_query_warn_rule.value;
		}
		if(form.obj_name_query_warn_rule.value!=0){
			data.obj_name =  form.obj_name_query_warn_rule.value;
		}
		if(form.kpi_name_query_warn_rule.value!=0){
			data.kpi_name =  form.kpi_name_query_warn_rule.value;
		}
		$('#queryBtn_warn_rule').attr("disabled", "true");
		$('#warn_rule_table').dataTable().api().destroy();
		dataTableConfig.ajax={"url": url, "data":data};
		dataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_warn_rule').removeAttr("disabled");
		};
		table=$('#warn_rule_table').dataTable(dataTableConfig);
	};
	
	$scope.objTypeChange = function(){
		document.getElementById("type_id_warn_rule").options[0].selected=true;
		document.getElementById("type_id_warn_rule").options[0].value = "";
		document.getElementById("type_id_warn_rule").options[0].text= "";
		var objTypeSelect =document.getElementById("obj_type_warn_rule");
		if(objTypeSelect.value=="0"){
			$('#type_id_warn_rule').removeAttr("disabled");
			$http({
				url : '/deviceManager/getDeviceTypeList',
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				$scope.deviceTypeList = data;
			}).error(function(data, header, config, status) {
				// 处理响应失败
			});
			$scope.objList = {};
		}else if(objTypeSelect.value=="1"){
			$scope.deviceTypeList = {};
			$('#type_id_warn_rule').attr("disabled", "true");
			var objType = objTypeSelect.value;
			$http({
				url : '/warn_rule/getObjList',
				params: {objType:objType},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				$scope.objList = data;
			}).error(function(data, header, config, status) {
				// 处理响应失败
			});
		}
	};
	
	$scope.typeIdChange = function(){
		document.getElementById("obj_id_warn_rule").options[0].selected=true;
		document.getElementById("obj_id_warn_rule").options[0].value = "";
		document.getElementById("obj_id_warn_rule").options[0].text= "";
		var objType = "0";
		var typeIdSelect =document.getElementById("type_id_warn_rule");
		var typeId = typeIdSelect.value;
		$http({
			url : '/warn_rule/getObjList',
			params:{objType:objType, typeId:typeId},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			$scope.objList = data;
		}).error(function(data, header, config, status) {
			// 处理响应失败
		});
	}
	
	$scope.objTypeChange_update = function(){
		document.getElementById("type_id_update_warn_rule").options[0].selected=true;
		document.getElementById("type_id_update_warn_rule").options[0].value = "";
		document.getElementById("type_id_update_warn_rule").options[0].text= "";
		var objTypeSelect =document.getElementById("obj_type_update_warn_rule");
		if(objTypeSelect.value=="0"){
			$('#type_id_update_warn_rule').removeAttr("disabled");
			$http({
				url : '/deviceManager/getDeviceTypeList',
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				$scope.deviceTypeList = data;
			}).error(function(data, header, config, status) {
				// 处理响应失败
			});
			$scope.objList = {};
		}else if(objTypeSelect.value=="1"){
			$scope.deviceTypeList = {};
			$('#type_id_update_warn_rule').attr("disabled", "true");
			var objType = objTypeSelect.value;
			$http({
				url : '/warn_rule/getObjList',
				params: {objType:objType},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				$scope.objList = data;
			}).error(function(data, header, config, status) {
				// 处理响应失败
			});
		}
	};
	
	
	//kpi选择模态框相关的function
	$scope.kpi_query = function () {
		var url = '/kpiDefine/queryKpiDefine';
		var data = {};
		var form =document.getElementById("queryForm_kpi_define");  // $('#queryForm');
		if(form.code_query_kpi_define.value!=0){
			data.code =  form.code_query_kpi_define.value;
		}
		if(form.name_query_kpi_define.value!=0){
			data.name = form.name_query_kpi_define.value;
		}
		if(form.type_id_query_kpi_define.value!=0){
			data.type_id=  form.type_id_query_kpi_define.value;
		}
		if(form.is_use_query_kpi_define.value!=0){
			data.is_use =  form.is_use_query_kpi_define.value;
		}
		if(form.obj_type_query_kpi_define.value!=0){
			data.obj_type =  form.obj_type_query_kpi_define.value;
		}
		$('#queryBtn_kpi_define').attr("disabled", "true");
		$('#kpi_table').dataTable().api().destroy();
		kpiDataTableConfig.ajax={"url": url, "data":data};
		kpiDataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_kpi_define').removeAttr("disabled");
		};
		table=$('#kpi_table').dataTable(kpiDataTableConfig);
	};
	$scope.sel_kpi_model = function(){
		keepDataFlag = true;
		$('#kpi_table').dataTable().api().destroy();
		kpi_table = $('#kpi_table').DataTable(kpiDataTableConfig);
		$("#addModal_warn_rule").modal("toggle");
		$("#kpi_select_model").modal("toggle");
	};
	$scope.sel_kpi_commit = function(){
		var selKpiIds = "";
		$('[name=items_kpi]:checkbox:checked').each(function() {
			selKpiIds += $(this).val() + ";";
		});
		if(selKpiIds==""){
			$('#selKpiAlert_warn_rule').html("<strong>请选择KPI记录！</strong>");
			$('#selKpiAlert_warn_rule').fadeIn(2000);
			$('#selKpiAlert_warn_rule').fadeOut(2000);
		}else if(selKpiIds.split(";").length>2){
			$('#selKpiAlert_warn_rule').html("<strong>选择的KPI记录数不能大于1.</strong>");
			$('#selKpiAlert_warn_rule').fadeIn(2000);
			$('#selKpiAlert_warn_rule').fadeOut(2000);
		}else{
			$("[name=items_kpi]:checkbox").each(function(key,value){
	            if($(value).prop('checked')){
	            	var selRow =$('#kpi_table').dataTable().fnGetData(key);
	    			var selKpiId = selRow.kpi_id;
	    			if(fromToUpdateModel){
	    				$('#kpi_id_update_warn_rule').val(selKpiId);
	    			}else{
	    				$('#kpi_id_warn_rule').val(selKpiId);
	    			}
	    			
	            }
			});
			$("#kpi_select_model").modal("toggle");
		}
	};
	$scope.sel_kpi_update_model = function(){
		keepDataFlag = true;
		fromToUpdateModel = true;
		$('#kpi_table').dataTable().api().destroy();
		kpi_table = $('#kpi_table').DataTable(kpiDataTableConfig);
		$("#updateModal_warn_rule").modal("toggle");
		$("#kpi_select_model").modal("toggle");
	};
	
	//Component模态框相关function
	$('#component_select_model').on('hide.bs.modal', function () {
		if(fromToUpdateModel){
			fromToUpdateModel = false;
			$('#updateModal_warn_rule').modal("toggle");
		}else{
			keepDataFlag = false;
			$('#addModal_warn_rule').modal("toggle");
		}
		
	});
	
	$scope.component_query = function () {
		var url = '/component/queryComponent';
		var data = {};
		var form =document.getElementById("queryForm_component");  // $('#queryForm');
		if(form.bean_class_query_component.value!=0){
			data.bean_class =  form.bean_class_query_component.value;
		}
		$('#queryBtn_component').attr("disabled", "true");
		
		$('#component_table').dataTable().api().destroy();
		componentDataTableConfig.ajax={"url": url, "data":data};
		componentDataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_component').removeAttr("disabled");
		};
		component_table=$('#component_table').dataTable(componentDataTableConfig);
	};
	$scope.sel_component_model = function(){
		keepDataFlag = true;
		$('#component_table').dataTable().api().destroy();
		component_table = $('#component_table').DataTable(componentDataTableConfig);
		$("#addModal_warn_rule").modal("toggle");
		$("#component_select_model").modal("toggle");
	};
	$scope.sel_component_commit = function(){
		var selComponentIds = "";
		$('[name=items_component]:checkbox:checked').each(function() {
			selComponentIds += $(this).val() + ";";
		});
		if(selComponentIds==""){
			$('#selComponentAlert_warn_rule').html("<strong>请选择组件记录！</strong>");
			$('#selComponentAlert_warn_rule').fadeIn(2000);
			$('#selComponentAlert_warn_rule').fadeOut(2000);
		}else if(selComponentIds.split(";").length>2){
			$('#selComponentAlert_warn_rule').html("<strong>选择的Component记录数不能大于1.</strong>");
			$('#selComponentAlert_warn_rule').fadeIn(2000);
			$('#selComponentAlert_warn_rule').fadeOut(2000);
		}else{
			$("[name=items_component]:checkbox").each(function(key,value){
	            if($(value).prop('checked')){
	            	var selRow =$('#component_table').dataTable().fnGetData(key);
	    			var selComponentId = selRow.id;
	    			if(fromToUpdateModel){
	    				$('#componet_update_warn_rule').val(selComponentId);
	    			}else{
	    				$('#componet_warn_rule').val(selComponentId);
	    			}
	            }
			});
			$("#component_select_model").modal("toggle");
		}
	};
	$scope.sel_component_update_model = function(){
		keepDataFlag = true;
		fromToUpdateModel = true;
		$('#component_table').dataTable().api().destroy();
		component_table = $('#component_table').DataTable(componentDataTableConfig);
		$("#updateModal_warn_rule").modal("toggle");
		$("#component_select_model").modal("toggle");
	};
	
	


	
	
	
	
	
	
	
	//接入点模态框相关function
	$('#access_select_model').on('hide.bs.modal', function () {
		if(fromToUpdateModel){
			fromToUpdateModel = false;
			$('#updateModal_warn_rule').modal("toggle");
		}else{
			keepDataFlag = false;
			$('#addModal_warn_rule').modal("toggle");
		}
		
	});
	
	$scope.component_query = function () {
		var url = '/access/queryAccess';
		var data = {};
		var form =document.getElementById("queryForm_access");  // $('#queryForm');
		if(form.type_code_query_access.value!=0){
			data.type_code =  form.type_code_query_access.value;
		}
		$('#queryBtn_access').attr("disabled", "true");
		$('#access_table').dataTable().api().destroy();
		accessDataTableConfig.ajax={"url": url, "data":data};
		accessDataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_access').removeAttr("disabled");
		};
		access_table=$('#access_table').dataTable(accessDataTableConfig);
	};
	$scope.sel_access_model = function(){
		keepDataFlag = true;
		$('#access_table').dataTable().api().destroy();
		access_table = $('#access_table').DataTable(accessDataTableConfig);
		$("#addModal_warn_rule").modal("toggle");
		$("#access_select_model").modal("toggle");
	};
	$scope.sel_access_commit = function(){
		var selAccessIds = "";
		$('[name=items_access]:checkbox:checked').each(function() {
			selAccessIds += $(this).val() + ";";
		});
		if(selAccessIds==""){
			if(fromToUpdateModel){
				$('#access_update_warn_rule').val("");
			}else{
				$('#access_warn_rule').val("");
			}
			$("#access_select_model").modal("toggle");
		}else if(selAccessIds.split(";").length>2){
			$('#selAccessAlert_warn_rule').html("<strong>选择的Access记录数不能大于1.</strong>");
			$('#selAccessAlert_warn_rule').fadeIn(2000);
			$('#selAccessAlert_warn_rule').fadeOut(2000);
		}else{
			$("[name=items_access]:checkbox").each(function(key,value){
	            if($(value).prop('checked')){
	            	var selRow =$('#access_table').dataTable().fnGetData(key);
	    			var selAccessId = selRow.id;
	    			if(fromToUpdateModel){
	    				$('#access_update_warn_rule').val(selAccessId);
	    			}else{
	    				$('#access_warn_rule').val(selAccessId);
	    			}
	            }
			});
			$("#access_select_model").modal("toggle");
		}
	};
	$scope.sel_access_update_model = function(){
		keepDataFlag = true;
		fromToUpdateModel = true;
		$('#access_table').dataTable().api().destroy();
		access_table = $('#access_table').DataTable(accessDataTableConfig);
		$("#updateModal_warn_rule").modal("toggle");
		$("#access_select_model").modal("toggle");
	};
});

var kpiDataTableConfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/kpiDefine/queryKpiDefine",
		columns : [
				{
					data : "kpi_id",
					render : function(data, type, full) {
						return '<input name=\"items_kpi\" type="checkbox" id=\"check_'+data+'\" value="' + data + '"/>';
					}
				}, {
					"data" : "kpi_id"
				},{
					"data" : "code"
				}, {
					"data" : "kpi_name"
				}, {
					"data" : "device_type_name"
				}, {
					"data" : "device_name"
				},{
					"data": "is_use_comment",
				},{
					"data": "value_type",
				},{
					"data": "obj_type_comment",
				},{
					"data": "device_type_id",
				},{
					"data": "obj_id",
				},{
					"data": "is_use",
				},{
					"data": "obj_type",
				},{
					"data": "bit"
				}],
		columnDefs: [    
		        {    
		        	"targets": [9,10,11,12,13],    
		        	"visible": false    
		        }], 
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",
};
//初始化表格
var kpi_table = $('#kpi_table').DataTable(kpiDataTableConfig);


//组件选择模态框table加载配置
var componentDataTableConfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/component/queryComponent",
		columns : [
				{
					data : "id",
					render : function(data, type, full) {
						return '<input name=\"items_component\" type="checkbox" id=\"check_'+data+'\" value="' + data + '"/>';
					}
				}, {
					"data" : "id"
				},{
					"data" : "bean_class"
				}, {
					"data" : "bean_class_path"
				}, {
					"data" : "state_name"
				}, {
					"data" : "comment"
				}, {
					"data" : "url_path"
				}, {
					"data" : "state"
				}, {
					"data" : "create_date"
				}],
		columnDefs: [{    
		             	"targets": [7,8], 
						"visible": false    
		             }], 
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",
};

//初始化表格
var component_table = $('#component_table').DataTable(componentDataTableConfig);


//接入点模态框表格
var accessDataTableConfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/access/queryAccess",
		columns : [
				{
					data : "id",
					render : function(data, type, full) {
						return '<input name=\"items_access\" type="checkbox" id=\"check_'+data+'\" value="' + data + '"/>';
					}
				}, {
					"data" : "id"
				},{
					"data" : "type_code"
				}, {
					"data" : "ip"
				}, {
					"data" : "port"
				}, {
					"data" : "snmp_commity"
				}, {
					"data" : "url"
				}, {
					"data" : "state_name"
				}, {
					"data" : "create_date"
				}, {
					"data" : "modify_date"
				}, {
					"data" : "sign"
				}, {
					"data" : "state"
				}, {
					"data" : "username"
				}, {
					"data" : "password"
				}],
		columnDefs: [{    
		             	"targets": [8,9,10,11,12,13], 
						"visible": false    
		             }], 
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",
};
//初始化表格
var access_table = $('#access_table').DataTable(componentDataTableConfig);


